package com.tqjc.system.core.task;

import cn.hutool.core.collection.CollectionUtil;
import com.tqjc.system.common.constant.ConfigConstant;
import com.tqjc.system.common.constant.GlobalConstant;
import com.tqjc.system.common.constant.RemoteURLConstant;
import com.tqjc.system.common.entity.BO.CarExecuteInfoBO;
import com.tqjc.system.common.entity.BO.ResponseLabelDataBO;
import com.tqjc.system.common.entity.VO.CarExecuteInfoVO;
import com.tqjc.system.common.enums.CarInfoVOEnum;
import com.tqjc.system.common.enums.OrderTypeEnum;
import com.tqjc.system.common.enums.TankCarNoEnum;
import com.tqjc.system.common.enums.TaskStatusEnum;
import com.tqjc.system.core.util.PLCHttpsUtils;
import com.tqjc.system.core.websocket.TaskExecutionWebSocketService;
import com.tqjc.system.system.service.SysAddDetailTaskService;
import com.tqjc.system.system.util.SystemUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.websocket.Session;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:  天车监控任务执行任务
 * @Author: 86195
 * @Date: 2024/1/17 20:17
 * @param:
 * @return:
 **/
@Component
@Slf4j
public class TaskExecutionInfoTask {

    @Autowired
    private TaskExecutionWebSocketService taskExecutionSocketService;

    @Autowired
    private SysAddDetailTaskService detailTaskService;


    //每5秒
    @Scheduled(cron = "0/1 * * * * ?")
    public void run(){

        Map<String, Session> sessionMap = taskExecutionSocketService.getSessionMap();
        //得到天车执行任务信息
        List<CarExecuteInfoBO> list = detailTaskService.getCarExecuteInfo(LocalDateTime.now(), TaskStatusEnum.STATUS_DOING.getStatus());
        List<CarExecuteInfoVO> res = new ArrayList<>(4);
        for (CarInfoVOEnum value : CarInfoVOEnum.values()) {
            CarExecuteInfoVO carExecuteInfoVO = new CarExecuteInfoVO();
            carExecuteInfoVO.setCarName(value.getCarName());
            carExecuteInfoVO.setNo(value.getNo());
            Map<String, String> map = SystemUtils.Sys_carInfo.get(ConfigConstant.CONFIG_TYPE_CAR_BAK_SN);
            String sn = map.get(value.getCarName());
            Map<String,String> paramMap = new HashMap<>(2);
            paramMap.put(GlobalConstant.STRING_SN,sn);
            paramMap.put(GlobalConstant.STRING_LABEL,value.getModelLable());
            String currentVal = this.getPLCVal(paramMap);
            if(GlobalConstant.VISIT_MODEL_FLAG){
                carExecuteInfoVO.setModelType("自动");
            } else {
                carExecuteInfoVO.setModelType("true".equals(currentVal) ? "自动":"手动");
            }


            //读取大车目标位置
            paramMap.put(GlobalConstant.STRING_LABEL,value.getXIndexTargetLabel());
            String xIndexTarget = this.getPLCVal(paramMap);
            carExecuteInfoVO.setXIndexTarget(xIndexTarget);

            //读取大车实际位置
            paramMap.put(GlobalConstant.STRING_LABEL,value.getXIndexActLabel());
            String xIndexAct = this.getPLCVal(paramMap);
            carExecuteInfoVO.setXIndexAct(xIndexAct);

            //读取小车目标位置
            paramMap.put(GlobalConstant.STRING_LABEL,value.getYIndexTargetLabel());
            String yIndexTarget = this.getPLCVal(paramMap);
            carExecuteInfoVO.setYIndexTarget(yIndexTarget);
            //读取小车实际位置
            paramMap.put(GlobalConstant.STRING_LABEL,value.getYIndexActLabel());
            String yIndexAct = this.getPLCVal(paramMap);
            carExecuteInfoVO.setYIndexAct(yIndexAct);

            //读取起升高度实际位置
            paramMap.put(GlobalConstant.STRING_LABEL,value.getZIndexActLabel());
            String zIndexAct = this.getPLCVal(paramMap);
            carExecuteInfoVO.setZIndexAct(zIndexAct);

            //读取起升高度目标位置
            paramMap.put(GlobalConstant.STRING_LABEL,value.getZIndexTargetLabel());
            String zIndexTarget = this.getPLCVal(paramMap);
            carExecuteInfoVO.setZIndexTarget(zIndexTarget);

            if(CollectionUtil.isNotEmpty(list)){
                list.forEach( e -> {
                    if(value.getCarName().equals(e.getCarName())){

                        //读取状态值
                        paramMap.put(GlobalConstant.STRING_LABEL,value.getStatusLable());
                        String val = this.getPLCVal(paramMap);
                        carExecuteInfoVO.setStatus(OrderTypeEnum.getDescByOrderType(val));
                        //天车名称相同，设值
                        carExecuteInfoVO.setMaterialName(e.getMaterialName());
                        carExecuteInfoVO.setTankCarNo(TankCarNoEnum.getDescByCarNo(e.getTankCarNo()));
                    }
                });
            }

            res.add(carExecuteInfoVO);
        }

        if(CollectionUtil.isNotEmpty(sessionMap)){
            taskExecutionSocketService.sendMessage(res,"操作成功",GlobalConstant.HTTPSTATUS_SUCCESS);
        }

    }

    private String getPLCVal(Map<String, String> paramMap) {
        ResponseLabelDataBO statusDataBO = PLCHttpsUtils.sendPost(RemoteURLConstant.URL_READBC, paramMap, ResponseLabelDataBO.class);
        Map<String, String> content = statusDataBO != null? statusDataBO.getContent(): null;
        if(content != null){
            return content.get("currentVal") != null ? content.get("currentVal").toLowerCase() : null;
        }
        return null;
    }
}
